﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WarehouseManager.DataAccess;
using WarehouseManager.Model;

namespace WarehouseManager.View
{
    public partial class FormPhieunhap : Form
    {
        PhieunhapAccess phieunhapAccess = new PhieunhapAccess();
        NhaCCAcess nhaccAccess = new NhaCCAcess();
        NhanvienAccess nhanvienAccess = new NhanvienAccess();
        public FormPhieunhap()
        {
            InitializeComponent();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                txtMapn.ReadOnly = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;
                btnThem.Enabled = true;
            }
            else
            {
                txtMapn.ReadOnly = true;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;
                btnThem.Enabled = false;
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMapn.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            cbNhacc.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            dtNgaynhap.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            cbManv.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
        }
        public void loadMancc()
        {
            DataTable dt = new DataTable();
            dt = nhaccAccess.LayNCC();
            DataRow dr;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dr = dt.Rows[i];
                cbNhacc.Items.Add(dr["maNCC"].ToString());
            }
        }
        public void loadManv()
        {
            DataTable dt = new DataTable();
            dt = nhanvienAccess.Laytoanbonhanvien();
            DataRow dr;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dr = dt.Rows[i];
                cbManv.Items.Add(dr["maNV"].ToString());
            }
        }
        private void FormPhieunhap_Load(object sender, EventArgs e)
        {
            loadMancc();
            loadManv();
            dataGridView1.DataSource = phieunhapAccess.laytoanbophieunhap();
        }

        private void txtTimkiem_TextChanged(object sender, EventArgs e)
        {
            dataGridView1.DataSource = phieunhapAccess.timkiemMapn(txtTimkiem.Text);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Phieunhap pn = new Phieunhap(txtMapn.Text, cbNhacc.Text, DateTime.Parse(dtNgaynhap.Text), cbManv.Text);
            phieunhapAccess.themPhieunhap(pn);
            dataGridView1.DataSource = phieunhapAccess.laytoanbophieunhap();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            Phieunhap pn = new Phieunhap(txtMapn.Text, cbNhacc.Text, DateTime.Parse(dtNgaynhap.Text), cbManv.Text);
            phieunhapAccess.suaPhieunhap(pn);
            dataGridView1.DataSource = phieunhapAccess.laytoanbophieunhap();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            Phieunhap pn = new Phieunhap(txtMapn.Text, cbNhacc.Text, DateTime.Parse(dtNgaynhap.Text), cbManv.Text);
            phieunhapAccess.xoaPhieunhap(pn);
            dataGridView1.DataSource = phieunhapAccess.laytoanbophieunhap();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.CurrentCell.ColumnIndex == 0)
            {
                FormNhapHang formNhaphang = new FormNhapHang(dataGridView1.CurrentRow.Cells[0].Value.ToString());
                formNhaphang.Show();
            }
        }
    }
}
